package com.hengyu.oauth2.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 * 项目名称：OAuth2Example
 * 项目版本：V1.0
 * 包名称：com.example.oauth.bean
 * 创建人：yuqy
 * 创建时间：2017/3/16 18:28
 * 修改人：yuqy
 * 修改时间：2017/3/16 18:28
 * 修改备注：
 */
@Entity
@Table(name = "oauth_user_details")
public class HengYuOAuth2UserDetails implements UserDetails{

    @Id
    @GeneratedValue
    private Long id;
    @Column(name = "user_name")
    private String userName;
    @Column(name = "user_password")
    private String userPassword;
    @Column(name = "user_status")
    private int status;
    @Column(name = "user_authorities")
    private String authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        String authorities = this.authorities;
        GrantedAuthority grantedAuthority = new GrantedAuthority() {
            @Override
            public String getAuthority() {
                return authorities;
            }
        };
        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        grantedAuthorities.add(grantedAuthority);
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return this.userPassword;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        //大于0表示正常
        return this.status > 0 ? true : false;
    }
}
